<%@page import="tools.MyTool"%>
<%@page import="java.util.Iterator"%>
<%@page import="accessor.HomeProductAccessor"%>
<%@page import="accessor.Products"%>
<%@include file="no_banner_header.jsp" %>
<script type="text/javascript">
    $(document).ready(function(){
        $("a#add_to_cart").click(function(){
            var product_id = $(this).attr("href");
            var product_name = $("#product_name_"+product_id).val();
            var product_image = $("#product_image_"+product_id).val();
            var price = $("#price_"+product_id).val();
            $.ajax({
                type: 'POST',
                url: '../do_add_cart',
                data: {
                    'productID' : product_id,
                    'productName' : product_name,
                    'defaultImage' : product_image,
                    'quantity' : 1,
                    'price' : price
                },
                dataType:"json",
                success: function(data)                          
                {     
                    $('#cart_count').html(data);                            
                    alert('You have put into your cart '+1+' product');
                }
            });  
            return false;
        });
    });  
</script>
<%
    int categoryID = request.getParameter("cateID") != null ? Integer.parseInt(request.getParameter("cateID")) : 0;
    HomeProductAccessor homeProductAccessor = new HomeProductAccessor();
    List<Products> products = homeProductAccessor.getByCategories(categoryID);
    int perPage = 10;
    int offSet = request.getParameter("offSet") != null ? Integer.parseInt(request.getParameter("offSet")) : 0;
    int endIndex = offSet + perPage;
    int totalRows = products.size();
    int totalPage = totalRows % perPage == 0 ? totalRows / perPage : totalRows / perPage + 1;
    int pageNo = request.getParameter("pageNo") != null ? Integer.parseInt(request.getParameter("pageNo")) : 1;
    String url = "listing.jsp?cateID=" + categoryID;
%>
<div class="content_bar">Product by Category</div>
<div class="break_line"></div>                                                                               
<div class="break_line"></div>                                                                               
<div id="paging" style="float: left;">
    <%=new MyTool().createLinks(url, totalRows, perPage, offSet, pageNo)%>
</div>
<div class="break_line"></div> 
<%for (int i = 0; i < products.size(); i++) {
        if (i >= offSet && i < endIndex) {
            Products product = products.get(i);
%>
<div class="<%=i % 2 == 0 ? "product_item" : "product_item right"%>">
    <%
        MyTool tool = new MyTool();
        String description = tool.getShortDescription(product.getDescription(), 24);
    %>
    <div class="product_item_left">
        <a href="details.jsp?productID=<%=product.getProductID()%>"><img src="../assets/img/p/<%=product.getDefaultImage()%>" width="128" height="128" style="border: 0px solid #DDDDDD;" /></a>
    </div>
    <div class="product_item_right">                            
        <h3><a href="#" title="ac"><%=product.getProductName()%></a></h3>
        <div class="product_item_desc">
            <%=description%>
            <div class="break_line"></div>
            <div class="price">Price: $<%=product.getPrice()%></div>
        </div>
            <input type="hidden" id="product_name_<%=product.getProductID()%>" name="product_name_<%=product.getProductID()%>" value="<%=product.getProductName()%>" />
            <input type="hidden" id="product_image_<%=product.getProductID()%>" name="product_image_<%=product.getProductID()%>" value="<%=product.getDefaultImage()%>" />
            <input type="hidden" id="price_<%=product.getProductID()%>" name="price_<%=product.getProductID()%>" value="<%=product.getPrice()%>" />
        <a id="add_to_cart" class="button_short" href="<%=product.getProductID()%>">Add to cart</a>
    </div>
</div>
<%}
    }%>
<div class="break_line"></div>                                                                               
<div id="paging" style="float: right;">
    <%=new MyTool().createLinks(url, totalRows, perPage, offSet, pageNo)%>
</div>
<div style="clear: both;">&nbsp;</div>
<%@include file="footer.jsp" %>